package com.feng.service.Impl;

import com.feng.dao.CarDao;
import com.feng.dao.GoodDao;
import com.feng.dao.StoreDao;
import com.feng.dto.Result;
import com.feng.pojo.Car;
import com.feng.pojo.Good;
import com.feng.pojo.Store;
import com.feng.service.CarService;
import com.feng.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CarServiceImpl implements CarService {

    @Autowired
    CarDao carDao;
    @Autowired
    GoodDao goodDao;
    @Autowired
    StoreDao storeDao;
    @Autowired
    RedisService redisService;

    //获得该买家的所有购物车商品
    @Override
    public Result getSortGood(Integer uid) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, List<Good>> sortGood = new HashMap<>();
        Map<Integer, Integer> number;
        List<Car> cars = carDao.findByUid(uid);
        number = cars.stream()
                .collect(Collectors.toMap(car -> car.getGoodId(), car -> car.getNumber()));
        List<Integer> goodIds = cars.stream()
                .map(car -> car.getGoodId())
                .collect(Collectors.toList());
        if (goodIds.isEmpty()) {
            return Result.builder().success(false).massage("该用户购物车没有商品").build();
        }
        List<Good> allGood = goodDao.findByGoodIds(goodIds);
        Set<Integer> allStoreIds = allGood.stream()
                .map(good -> good.getStoreId())
                .collect(Collectors.toSet());
        List<Store> allStore = storeDao.findByStoreIds(allStoreIds);
        List<Good> tempList = null;
        for (Store store : allStore) {
            tempList = allGood.stream()
                    .filter(good -> good.getStoreId().equals(store.getId()))
                    .collect(Collectors.toList());
            sortGood.put(store.getName(), tempList);
        }
        resultMap.put("good", sortGood);
        resultMap.put("num", number);
        return Result.builder().success(true).massage("获取成功").data(resultMap).build();
    }

    @Override
    public Result getSortGood2(Integer uid) {
        Map<String, Object> resultMap = new HashMap<>();
        Map<String, List<Good>> sortGood = new HashMap<>();
        Map<Integer, Integer> number;
        List<Car> cars = (List<Car>) redisService.get("car" + uid);
        //获得goodId-number的map
        number = cars.stream().collect(Collectors.toMap(car -> car.getGoodId(), car -> car.getNumber()));
        List<Integer> goodIds = cars.stream()
                .map(car -> car.getGoodId())
                .collect(Collectors.toList());
        if (goodIds.isEmpty()) {
            return Result.builder().success(false).massage("该用户购物车没有商品").build();
        }
        List<Good> allGood = goodDao.findByGoodIds(goodIds);
        Set<Integer> allStoreIds = allGood.stream()
                .map(good -> good.getStoreId())
                .collect(Collectors.toSet());
        List<Store> allStore = storeDao.findByStoreIds(allStoreIds);
        List<Good> tempList = null;
        for (Store store : allStore) {
            tempList = allGood.stream()
                    .filter(good -> good.getStoreId().equals(store.getId()))
                    .collect(Collectors.toList());
            sortGood.put(store.getName(), tempList);
        }
        resultMap.put("good", sortGood);
        resultMap.put("num", number);
        return Result.builder().success(true).massage("获取成功").data(resultMap).build();
    }

    @Override
    public Result putInCar(Integer goodId, Integer repertory, Integer number, Integer uid) {
        if (number > repertory) {
            return Result.builder().success(false).massage("加入购物车数量大于库存").build();
        }
        Car car = new Car(null, goodId, uid, number);
        List<Car> cars = null;
        if (redisService.hasKey(uid.toString())) {
            cars = (List<Car>) redisService.get("car" + uid);
        } else {
            cars = new ArrayList<>();
        }
        cars.add(car);
        redisService.set("car" + uid, cars);
        return Result.builder().success(true).massage("成功加入购物车").build();
    }
}
